####################################################################################
###
###   Immigration, Public Housing and Support for the French National Front
###   Gloria Gennaro
###
###   Paper Figure A12, A13, A14, A15
###
####################################################################################


library(dplyr)
library(AER)
library(rlist)
library(ggplot2)


################################################################################
# Set Up
################################################################################

# Load Working Directories
source(paste0(wd_main, '/2_code/00_working_directories.R'))

# Load data and clean
source(paste0(wd_code, '/05_data_load_clean_absun.R'))

# Merge with outflows data
temp = read.csv(paste0(wd_data, '/residential_out_flows.csv'))
df_did = merge(df_did, temp, on=c('CODGEO', 'year'), all.x=T)

rm(temp)


################################################################################
# Plot Functions
################################################################################

makeplot = function(model_output, hete_var){
   groups = c(paste0('Low ', hete_var), paste0('Medium ', hete_var), paste0('High ', hete_var))
   container = lapply(model_output, function(x) rbind(cbind(x[2:5,], period = c(-1, 1, 2,3)), c(0,0,NA,NA,0)))
   container = list.rbind(container) %>% as.data.frame()
   container = cbind(container, group=rep(groups, each=5))
   colnames(container) = c('beta', 'se', 'tval', 'pval', 'period', 'group')
   container$group.f = factor(container$group, levels = groups)
   ggplot(data=container, aes(y=beta, x=factor(period), colour=group.f, shape = group.f))+
      geom_point(position = position_dodge(width = 0.5)) + 
      geom_errorbar(aes(ymin=beta-1.96*se, ymax=beta+1.96*se), width=.1, position = position_dodge(width = 0.5)) +
      geom_hline(yintercept=0, colour='red')+
      xlab("Periods to Policy Application") + 
      ylab("") +
      theme_light()+
      theme(legend.position="bottom", legend.title = element_blank(), text = element_text(size=18))
}



################################################################################
# Figure A12
################################################################################

modelli = list()
for (n in c(1,2,3)){
   print(paste0('Immigation tercile ', n, ' -------------- '))
   df1 = df_did[which(!is.na(df_did$moveout) & df_did$imm_quant_99==n),]
   temp = lm(data=df1, moveout ~   m1 + p1 + p2 + p3 + running + 
                factor(CODGEO) + factor(policy_period) )
   temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
   modelli[[n]] = temp
}

makeplot(modelli, 'Immigration')
ggsave(paste0(wd_res, '/figures/figA12.pdf'), height=7, width=10)



################################################################################
# Figure A13
################################################################################

modelli = list()
for (n in c(1,2,3)){
   print(paste0('Immigation tercile ', n, ' -------------- '))
   df1 = df_did[which(!is.na(df_did$moveout) & df_did$imm_quant_99==n),]
   temp = lm(data=df1, moveout_to_no_sru ~   m1 + p1 + p2 + p3 + running + 
                factor(CODGEO) + factor(policy_period) )
   temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
   modelli[[n]] = temp
}

makeplot(modelli, 'Immigration')
ggsave(paste0(wd_res, '/figures/figA13.pdf'), height=7, width=10)


################################################################################
# Figure A14
################################################################################

modelli = list()
for (n in c(1,2,3)){
   print(paste0('Immigation tercile ', n, ' -------------- '))
   df1 = df_did[which(!is.na(df_did$moveout_french) & df_did$imm_quant_99==n),]
   temp = lm(data=df1, I(moveout_immigrants) ~   m1 + p1 + p2 + p3 + running + 
                factor(CODGEO) + factor(policy_period) )
   temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
   modelli[[n]] = temp
}

makeplot(modelli, 'Immigration')
ggsave(paste0(wd_res, '/figures/figA14.pdf'), height=7, width=10)



################################################################################
# Figure A15
################################################################################

modelli = list()
for (n in c(1,2,3)){
   print(paste0('Immigation tercile ', n, ' -------------- '))
   df1 = df_did[which(!is.na(df_did$moveout) & df_did$imm_quant_99==n),]
   temp = lm(data=df1, moveout_to_no_sru_french ~   m1 + p1 + p2 + p3 + running +
                factor(CODGEO) + factor(policy_period) )
   temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
   modelli[[n]] = temp
}

makeplot(modelli, 'Immigration')
ggsave(paste0(wd_res, '/figures/figA15.pdf'), height=7, width=10)

